-
Notifications
You must be signed in to change notification settings - Fork 43
Support z separate/no-separate code #707
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
quic-seaswara
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we import the tests from lld for this as part of this PR ?
`-z separate-code` puts the text segment in its own pages disjoint from any other segments.
quic-seaswara
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have other options that are not compatible with -z seperate-code ?
How does using PHDR's, MEMORY, linker scripts, Adding LMA addresses, Using linker special directives (BYTE, QUAD, SQUAD, etc), orphan section placements work with this option ? Can you explore more with using the bfd linker ?
How does -z relro work this option ?
We might need to add a FAQ also for cases that people might want to consider this option for image size constraints, adding a new segment can cause increase in image size for example.
These analysis could be seperate bugs, and it might deserve an epic as applicable.
Does these -z options translate to reproduce command line option appropriately ?
Can we document incompatibilities ? and also document this option in detail.
I would prefer a test that can build and run in addition to a lit unit test.
You can decide to make some of these as follow ups with some discretion.
| bool PrevExec = (PrevSegIt->second.front()->flag() & llvm::ELF::PF_X); | ||
| return PrevExec != IsExec; | ||
| }; | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we rename this to something better since the lambda does nore than aligning segments to a page ?
Why does aligning segments to a page not work with -z seperate-code ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we rename this to something better since the lambda does nore than aligning segments to a page ?
If it does anything more than determine whether or not segments should be page aligned then I did something wrong. Taking another look, I am not seeing that it does anything other than that. What am I missing? What name do you suggest?
Why does aligning segments to a page not work with -z seperate-code ?
I am not sure if I understand your question. It does "work". Starting code segments at page aligned offsets ensures that they are not co-resident on the same page as other segments. That is the point of this lambda function: when transitioning to/from a code segment to another kind of segment, with -z separate-code the next segment should be page aligned. That is my understanding anyways
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The lambda checks for the variety of things
- align segments to a page
- checks for the last section added to the segment and sees if it has execute permissions (SeperateSegmentKind for code)
Should this be called DoPageAlignment or NeedSeperateSegmentOrPageAlign ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
checks for the last section added to the segment and sees if it has execute permissions (SeperateSegmentKind for code)
Because it is relevant to determine if we ShouldPageAlignSegments?
It checks if we should page align segments based on:
--align-segments/no-align-segments- if we have a linker script
-z separate-code. This requires looking at transitions to/from executable segments
Should this be called DoPageAlignment or NeedSeperateSegmentOrPageAlign
I don't see how these are better names. They sound like (worse) synonyms to me.
partaror
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This option is affecting all the targets, however, the test is added just for x86. Can we add a test in the common/Standalone subdirectory as well?
| NoExecStack, | ||
| NoGnuStack, | ||
| SeparateCode, | ||
| NoseparateCode, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this be better?
| NoseparateCode, | |
| NoSeparateCode, |
|
|
||
| auto SeparateKind = config().options().getSeparateSegmentKind(); | ||
| bool ShouldSeparate = | ||
| !linkerScriptHasSectionsCommand && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that the -z separate-code option only takes affect when the linker script does not have SECTIONS command. Should we report a warning if this option is used along with the SECTIONS command?
|
I took some time to write test cases, it looks like there is some behavioral difference with these switches with linker script, omagic support, and shared library support. @quic-areg can you please look into this usage, though we dont need to copy exact bfd behavior in cases where the linker gets confused and creates a segment that mixes R/RW/RWX. Please see this test and let me know how we plan to address this both in diagnostics and other comments we have raised. |
It is expected that mixing |
Mixing -z seperate-code with omagic makes the linker choose omagic behavior, if you observe. For shared libraries -z separate-code is not effective, if you see. Since -z relro also bumps the page alignment for certain sections when they are seen in the program layout, doesn't it conflict ? I am not sure why it does not make sense with linker scripts, does -z separate-code option always create aligned segments to page boundaries ? |
Yes I observed this. It makes no sense to request an RWX segment (omagic) along with a separate code segment. The options should not be mixed. both eld,lld,and bfd behave the same way when they are mixed (omagic takes precedence), if you observe:
I guess I am missing something. Can you show an example? here's my example, if you see The code segments are all in their own pages; it looks 'effective' to me. The other segments are allowed to overlap.
eld page-aligns segments by default when there is no linker script present, so the effect of
No I don't think so. Do you have an example?
If the user has a linker script then they should own the layout/page alignment decisions. eg we page align segments by default when there is no linker script, but this is not turned on with linker scripts. lld does not honor
starting code segments at page aligned offsets is one way to ensure that they are not co-resident on the same page as any other segment, and that seems to be how the other linkers implement it. |
-z separate-codeputs the text segment in itsown pages disjoint from any other segments.